package com.zhl.media.app.mvp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhl.media.app.R;
import com.zhl.media.app.model.GroupInfo;
import com.zhl.media.commonres.view.CustomExpandableListView;

import java.util.List;


public class GroupAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private List<GroupInfo> groupDataList;

    public GroupAdapter(Context context, List<GroupInfo> groupDataList) {
        mContext = context;
        this.groupDataList = groupDataList;
    }

    @Override
    public int getGroupCount() {
        return groupDataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(groupDataList.get(groupPosition).getProjects().size()>0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupDataList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupDataList.get(groupPosition).getProjects().size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item,null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.tvTitle.setText(groupDataList.get(groupPosition).getName());
        if(getChildrenCount(groupPosition)==0){
            holder.ivArrow.setVisibility(View.GONE);
        }else {
            holder.ivArrow.setVisibility(View.VISIBLE);
            //设置指示器
            if (isExpanded) {
                holder.ivArrow.setImageResource(R.drawable.down);
            } else {
                holder.ivArrow.setImageResource(R.drawable.left);
            }
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ExpandableListView eListView = getExpandableListView();
        ProjectAdapter childAdapter = new ProjectAdapter(mContext, groupDataList.get(groupPosition).getProjects());
        eListView.setAdapter(childAdapter);
        return eListView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 动态创建子ExpandableListView
     */
    public ExpandableListView getExpandableListView() {
        CustomExpandableListView mExpandableListView = new CustomExpandableListView(mContext);
//        mExpandableListView.setDividerHeight(0);// 取消group项的分割线
//        mExpandableListView.setChildDivider(null);// 取消child项的分割线
        mExpandableListView.setGroupIndicator(null);// 取消展开折叠的指示图标
        return mExpandableListView;
    }

    static class GroupHolder {
        private TextView tvTitle;
        private ImageView ivArrow;

        public GroupHolder(View v) {
            tvTitle = (TextView) v.findViewById(R.id.tv_title);
            ivArrow = (ImageView) v.findViewById(R.id.iv_arrow);
        }
    }
}